package com.lagou.edu.factory.parsing;

import com.lagou.edu.factory.BeanFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;

public class XmlBeanBuilder implements BeanBuilder{

    private Element rootElement;


    public BeanPool parse(BeanPool beanPool) throws Exception{
        // 任务一：读取解析xml，通过反射技术实例化对象并且存储待用（map集合）
        // 加载xml
        InputStream resourceAsStream = BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml");


        // 解析xml
        SAXReader saxReader = new SAXReader();
        if (beanPool.getBeansMap() == null){
            beanPool.setBeansMap(new HashMap());
        }
        Document document = saxReader.read(resourceAsStream);
        rootElement = document.getRootElement();

        //解析basePackage
        String basePackage = "";
        List<Element> basePackageElem = rootElement.selectNodes("//component-scan");
        if (basePackageElem != null && basePackageElem.size() > 0){
            basePackage = basePackageElem.get(0).attributeValue("base-package");
        }
        beanPool.setBasePackage(basePackage);

        List<Element> beanList = rootElement.selectNodes("//bean");
        for (int i = 0; i < beanList.size(); i++) {
            Element element =  beanList.get(i);
            // 处理每个bean元素，获取到该元素的id 和 class 属性
            String id = element.attributeValue("id");        // accountDao
            String clazz = element.attributeValue("class");  // com.lagou.edu.dao.impl.JdbcAccountDaoImpl
            // 通过反射技术实例化对象
            Class<?> aClass = Class.forName(clazz);
            Object o = aClass.newInstance();  // 实例化之后的对象

            // 存储到map中待用
            beanPool.getBeansMap().put(id,o);
            // 使用类名作为key
            beanPool.getBeansMapWithClassName().put(clazz,o);

        }

        return beanPool;
    }

    @Override
    public BeanPool assemble(BeanPool beanPool) throws Exception {


        // 实例化完成之后维护对象的依赖关系，检查哪些对象需要传值进入，根据它的配置，我们传入相应的值
        // 有property子元素的bean就有传值需求
        List<Element> propertyList = rootElement.selectNodes("//property");
        // 解析property，获取父元素
        for (int i = 0; i < propertyList.size(); i++) {
            Element element =  propertyList.get(i);   //<property name="AccountDao" ref="accountDao"></property>
            String name = element.attributeValue("name");
            String ref = element.attributeValue("ref");

            // 找到当前需要被处理依赖关系的bean
            Element parent = element.getParent();

            // 调用父元素对象的反射功能
            String parentId = parent.attributeValue("id");
            Object parentObject = beanPool.getBeansMap().get(parentId);
            if (parentObject == null){
                System.out.println(parentId);
            }
            // 遍历父对象中的所有方法，找到"set" + name
            Method[] methods = parentObject.getClass().getMethods();
            for (int j = 0; j < methods.length; j++) {
                Method method = methods[j];
                if(method.getName().equalsIgnoreCase("set" + name)) {  // 该方法就是 setAccountDao(AccountDao accountDao)
                    method.invoke(parentObject, beanPool.getBeansMap().get(ref));
                }
            }

            // 把处理之后的parentObject重新放到map中
            beanPool.getBeansMap().put(parentId,parentObject);


        }
        return beanPool;
    }

}
